

#Brilliant.org Hunger Games prisoner's dilemma algorithm contest submission.
#Author: Greg Hamel (solo)
#Algorithm name: Mr. Karma

'''
Mr. Karma operates on the principle that individuals in a society need to be "nice but not too nice"
to survive. The rationale behind this principle is that slackers will eventually be recognized
for their antisocial behavior and met with high rates of defection, while highly cooperative players will
become targets or "patsies" for defectors. Both slackers and patsies will eventually die out.

Similar to tit-for-tat, Mr. Karma operates on the principle that cooperation should be determined, in part,
by the amount of cooperation it received in the previous round. Mr. Karma also attempts to maintain a
reputation score that is above the average and median for the population, since other players may decide
to base decisions on those metrics. When reputation is above the average and the median, Mr. Karma takes the opportunity
to cooperate slightly less than the amount it received in the previous round. If reputation dips too low, it attempts to
increase it quickly, but only if other players are cooperating at a reasonable rate (>=25%).
'''



def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):

    #If the game comes down to a battle to the death with 1 other player, choose the dominant strategy of slacking.
    if len(player_reputations) == 1:
        return ["s"]


    #If the game is in the first round, cooperate with all other players to establish a high initial reputation.
    if round_number==1:
        return ['h' for player in player_reputations]


    #Creates a list of the proper length for storing output.
    hunt_decisions = player_reputations[:]


    #During rounds 2-5, cooperate with anyone who has cooperated over 50% of the time.
    #This helps establish a good reputation and punishes greedy players.
    if round_number in range(2,6):
        total_matches+=len(player_reputations)
        for player in range(len(player_reputations)):
            if player_reputations[player] < 0.5:
                hunt_decisions[player] = "s"
            else:
                hunt_decisions[player] = "h"
        return hunt_decisions


    #Calculations to find the average and median reputations in the population.
    average_reputation = sum(player_reputations[:])/float(len(player_reputations))
    median_reputation = sorted(player_reputations[:])[len(player_reputations)//2]


    #Calculates the number of people who cooperated in the last round.
    last_round_coops = food_last_round.count(1)+food_last_round.count(0)


    #When reputation is above the average and median, cooperate based on the level of cooperation from the previous round.
    if current_reputation > average_reputation and current_reputation > median_reputation:
        last_round_coops-= 1+len(player_reputations)//10       #But subtract a small amount to avoid being overly nice.

       #Keep the return array unsorted so that cooperation is spread randomly to avoid rewarding players with any particular strategy.
        for player in range(0,last_round_coops):
            hunt_decisions[player] = "h"

        #Defect against all players remaining in the player list.
        for player in hunt_decisions:
            if player != "h":
                player = "s"
        return hunt_decisions

    #If current reputation is below the average or median, try to get back into the good graces
    #of other players quickly by cooperating with everyone, but only if at least 25%
    #cooperated in the last round.
    elif last_round_coops>=len(player_reputations)*0.25:
        return ['h' for player in player_reputations]

    #If below the average or median reputation and fewer than 25% are cooperating, defect against all players
    #to avoid rewarding greedy player who are not likely to cooperate in the future.
    else:
        return ['s' for player in player_reputations]


def hunt_outcomes(food_earnings):
    #Create global variable to track the amount of cooperation recieved in each round.
    global food_last_round
    food_last_round = food_earnings
    pass

def round_end(award, m, number_hunters):
    pass
